package cn.piesat.scanning.business.xz.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildHB_FY4A_YMWLServiceImpl implements BuildAlgorithmTaskService {
    @Autowired
    private HtScanningMetadataService htScanningMetadataService;

    public static void main(String[] args) {
        Date dateTime = DateUtil.offsetDay(new Date(), -1).toJdkDate();
        String format = DateUtil.format(dateTime, "yyyyMMddHHmmss");
        System.out.println(format);
    }

    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam,
                                                   AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate,
                                                   String scanningPath, HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        sb.append(primaryFile + ",");
        //FY4A玉米渍涝算法
        Map<String, Object> params = new HashMap<>();
        Date date = DateUtil.parse(fileDate, "yyyyMMddHHmmss").toJdkDate();
        for (int i = 1; i <= 9; i++) {
            Date dateTime = DateUtil.offsetDay(date, -i).toJdkDate();
            String format = DateUtil.format(dateTime, "yyyyMMddHHmmss");
            System.out.println(format);
            HtScanningMetadataDTO metadataDTO = htScanningMetadataService.findMetaDataDto(format, "L3", "FY4A",
                    "QPE", "130000", "TIFF", "BAS");
            if (null == metadataDTO && i == 3 ) {
                algorithmParams.put("parameter01", null);
                return algorithmParams;
            }
            if (null == metadataDTO) {
                sb.append("000,");
            } else {
                sb.append(taskPathParam.getParameter02() + metadataDTO.getFilePath() + ",");
            }
        }
        String s = StrUtil.removeSuffix(sb.toString(), ",");
        System.out.println("拼接文件为:"+s);
        params.put("QPEFilePath", s);
        params.put("Auxpath", taskPathParam.getParameter03());
        params.put("timeID", fileDate.substring(0, 8));
        params.put("Tempdir", taskPathParam.getParameter08());
        algorithmParams.put("parameter00", adsType);
        algorithmParams.put("parameter01", params);
        return algorithmParams;

    }
}
